from django.contrib import admin
from django.contrib.admin.templatetags.admin_list import result_list
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser, MajorModel, MajorSelection, LotteryResultModel


# @admin.register(MajorModel)
class MajorSelectInline(admin.TabularInline):
    model = MajorSelection  # 使用 UserGroup 作为中间模型
    extra = 1  # 默认添加1个空行供输入


@admin.register(CustomUser)
class CustomUserAdmin(UserAdmin):
    # 定制显示的字段
    fieldsets = UserAdmin.fieldsets + (
        ('用户分组', {'fields': ('nickname', 'result_list')}),
    )
    add_fieldsets = UserAdmin.add_fieldsets + (
        ('用户分组', {'fields': ('nickname', 'result_list')}),
    )

    search_fields = ['id', 'username', 'nickname', 'result_list']

    # 定制用户列表显示的字段
    list_display = ['id', 'username', 'nickname', 'result_list']  # 只显示 'nickname' 字段

    # 禁止编辑超级用户和权限组
    readonly_fields = ('is_superuser', 'groups')
    # 使用 UserGroupInline 作为内联表单展示
    inlines = [MajorSelectInline]


@admin.register(MajorModel)
class MajorModelAdmin(admin.ModelAdmin):
    list_display = ['name', 'level', 'start', 'end', 'num']  # 显示字段


@admin.register(LotteryResultModel)
class LotteryResultModelAdmin(admin.ModelAdmin):
    list_display = ('major', 'user', 'index_num')  # Ensure 'num' is displayed in the list
    search_fields = ('user__nickname', 'index_num')  # Enable search by user, index_num, or num
    list_filter = ('major',  'index_num')  # Filter options for admin
    fieldsets = (
        (None, {
            'fields': ('major', 'user', 'index_num')  # Add 'num' field to form view
        }),
    )
